package wnhz.com.business.rebateshop.fragment;

import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.base.MyBaseFragment;
import wnhz.com.business.groupshop.ui.GoodsDetailsActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.rebateshop.activity.home.RebateGoodsDetailsActivity;
import wnhz.com.business.rebateshop.activity.home.ShareFriandActivity;
import wnhz.com.business.uitls.CoverFlowLayoutManger;
import wnhz.com.business.uitls.GlideImageLoader;
import wnhz.com.business.uitls.RecyclerCoverFlow;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * Created by lizhenglong on 2017-09-18 0018.
 */

public class HomeFragment extends MyBaseFragment implements View.OnClickListener, ActionBarClickListener {
    @BindView(R.id.gally_rebatehome_list)//画廊列表
            RecyclerCoverFlow gally_list;
    @BindView(R.id.banner_rebatehome_banner)//首页banner图
            Banner banner_rebatehome_banner;
    @BindView(R.id.tv_rebatehome_goodsname)//画廊对应的商品名字
            TextView tv_rebatehome_goodsname;
    @BindView(R.id.tv_rebatehome_goodspeice)//画廊对应商品的价格
            TextView tv_rebatehome_goodspeice;
    @BindView(R.id.rl_rebatehome_share)//分享
            RelativeLayout rl_rebatehome_share;
    @BindView(R.id.ry_rebatehome_bootlist)//底部商品
            RecyclerView ry_rebatehome_bootlist;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;

    private MyBaseActivity activity;
    private BaseRVAdapter adapter1;
    private BaseRVAdapter adapter2;
    private List<String> urllist = new ArrayList<>();
    private List<String> bannerlist = new ArrayList<>();

    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what) {
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rebate_home, container, false);
        ButterKnife.bind(this, view);
        activity = (MyBaseActivity) getActivity();
        actionbar.setData("返利商城", 0, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        initTestGallylist();
        initTestBannerlist();
        initTestBootList();
        initGally();
        initBanner();
        initList();
        initview();
        return view;
    }

    private void initview() {
        rl_rebatehome_share.setOnClickListener(this);
    }

    /**
     * banner图
     */
    private void initBanner() {
        banner_rebatehome_banner.setImageLoader(new GlideImageLoader());
        banner_rebatehome_banner.setImages(bannerlist);
        banner_rebatehome_banner.start();
        bannerlist.clear();
    }

    private void initTestBootList() {

    }

    private void initTestGallylist() {
        urllist.add("http://img001.hc360.cn/m3/M07/05/03/wKhQ51SaLTuEe2HHAAAAAOAIbjU899.jpg");
        urllist.add("http://img10.360buyimg.com/imgzone/jfs/t997/136/225324268/119934/4afc99af/550b91baNadb73380.jpg");
        urllist.add("http://img14.360buyimg.com/popWaterMark/jfs/t1174/273/320820408/258105/1e76ff74/55229635Nbb32cc7f.jpg");
        urllist.add("http://img10.360buyimg.com/imgzone/jfs/t997/136/225324268/119934/4afc99af/550b91baNadb73380.jpg");
        urllist.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1803956466,2330317273&fm=27&gp=0.jpg");
        urllist.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2895095267,235755504&fm=27&gp=0.jpg");
        urllist.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=373796927,2570237301&fm=27&gp=0.jpg");
    }

    private void initTestBannerlist() {
        bannerlist.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1015767822,1125890338&fm=27&gp=0.jpg");
        bannerlist.add("http://img10.360buyimg.com/imgzone/jfs/t997/136/225324268/119934/4afc99af/550b91baNadb73380.jpg");
        bannerlist.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=373796927,2570237301&fm=27&gp=0.jpg");
        bannerlist.add("http://img14.360buyimg.com/popWaterMark/jfs/t1174/273/320820408/258105/1e76ff74/55229635Nbb32cc7f.jpg");
        bannerlist.add("http://img001.hc360.cn/m3/M07/05/03/wKhQ51SaLTuEe2HHAAAAAOAIbjU899.jpg");
        bannerlist.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=373796927,2570237301&fm=27&gp=0.jpg");
        bannerlist.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=373796927,2570237301&fm=27&gp=0.jpg");
    }

    /**
     * 画廊
     */
    private void initGally() {//需要判断是否为空,否则滑动崩溃
//        mList.setFlatFlow(true); //平面滚动
        gally_list.setGreyItem(true); //设置灰度渐变
//        mList.setAlphaItem(true); //设置半透渐变
        adapter1 = new BaseRVAdapter(activity, urllist) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_home_gally;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                Glide.with(activity).load(urllist.get(position)).into(holder.getImageView(R.id.img_home_gally));
                holder.getImageView(R.id.img_home_gally).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(RebateGoodsDetailsActivity.createIntent(activity, "", "", ""));
                    }
                });
            }
        };
        gally_list.setAdapter(adapter1);
        gally_list.setOnItemSelectedListener(new CoverFlowLayoutManger.OnSelected() {
            @Override
            public void onItemSelected(int position) {
                tv_rebatehome_goodsname.setText("" + position);
                tv_rebatehome_goodspeice.setText("¥" + position);
            }
        });
    }


    /**
     * 底部商品列表
     */
    private void initList() {
        ry_rebatehome_bootlist.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false));

        adapter2 = new BaseRVAdapter(activity, urllist) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_rebatehome_bootlist;
            }
            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.item_tv_rebatehome_name, "商品名称");
                holder.setTextView(R.id.item_tv_rebatehome_info, "简介简介简介简介简介");
                holder.setTextView(R.id.item_tv_rebatehome_price, "¥100");
                Glide.with(activity).load(urllist.get(position)).into(holder.getImageView(R.id.item_img_rebatehome));
                holder.getView(R.id.item_tv_rebatehome_share).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(ShareFriandActivity.createIntent(getContext()));
                    }
                });
            }
        };
        adapter2.addItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                startActivity(RebateGoodsDetailsActivity.createIntent(activity, "", "", ""));
            }
        });
        ry_rebatehome_bootlist.setAdapter(adapter2);
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //分享
            case R.id.rl_rebatehome_share:
                startActivity(ShareFriandActivity.createIntent(getContext()));
                break;
        }
    }
}
